// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-12 17:46
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.DateTrain;

import org.junit.jupiter.api.Test;

public class SearchInsert {
    /**
     * 定位元素或插入位置
     */
    public int searchInsert(int[] nums, int target) {
        // 使用二分法
        int n = nums.length;
        int left = 0, right = n;
        while (left < right) {
            int mid = left + (right - left) >>> 1;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }


    @Test
    public void shout() {
        int[] test = {1, 3, 5, 6};
        System.out.println(searchInsert(test, 7));
    }
}
